package com.zerui.manager.accessauthdevice.gatewayimpl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zerui.cola.dto.Response;
import com.zerui.manager.accessauthdevice.AccessAuthDevice;
import com.zerui.manager.accessauthdevice.convertor.AccessAuthDeviceConvert;
import com.zerui.manager.accessauthdevice.entity.AccessAuthDeviceDo;
import com.zerui.manager.accessauthdevice.gateway.AccessAuthDeviceGateWay;
import com.zerui.manager.accessauthdevice.mapper.AccessAuthDeviceMapper;
import com.zerui.manager.web.cmd.AccessAuthDeviceQryCmd;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author wh
 * @create_time 2023-08-23 15:28
 */
@Component
public class AccessAuthDeviceGateWayImpl implements AccessAuthDeviceGateWay {

    @Resource
    private AccessAuthDeviceMapper accessAuthDeviceMapper;

    @Override
    public Map<Long, AccessAuthDevice> getAccessAuthDeviceIds(Integer deviceFrom) {
        LambdaQueryWrapper<AccessAuthDeviceDo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccessAuthDeviceDo::getDeviceFrom, deviceFrom);
        List<AccessAuthDeviceDo> accessAuthDeviceDos = accessAuthDeviceMapper
                .selectList(queryWrapper);
        List<AccessAuthDevice> accessAuthDevices = AccessAuthDeviceConvert.INSTANCE.convertAccessAuthDeviceDosToAccessAuthDevices(accessAuthDeviceDos);
	    return accessAuthDevices.stream().collect(Collectors.toMap(AccessAuthDevice::getDeviceId, Function.identity(), (key1, key2) -> key2));
    }

    @Override
    public void insertAccessAuthDevice(List<AccessAuthDevice> accessAuthDevices) {
        if (CollectionUtils.isNotEmpty(accessAuthDevices)) {
            List<AccessAuthDeviceDo> accessAuthDeviceDos = AccessAuthDeviceConvert.INSTANCE.convertAccessAuthDevicesToAccessAuthDeviceDos(accessAuthDevices);
            accessAuthDeviceMapper.insertBatchSomeColumn(accessAuthDeviceDos);
        }
    }


    @Override
    public void deleteAccessAuthByDeviceIds(List<Long> removeIds) {
        if(CollectionUtils.isNotEmpty(removeIds)){
            LambdaQueryWrapper<AccessAuthDeviceDo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(AccessAuthDeviceDo::getDeviceId,removeIds);
            accessAuthDeviceMapper.delete(queryWrapper);
        }
    }

    @Override
    public Page<AccessAuthDevice> getPageList(AccessAuthDeviceQryCmd accessAuthDeviceQryCmd) {
        LambdaQueryWrapper<AccessAuthDeviceDo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(accessAuthDeviceQryCmd.getName()),AccessAuthDeviceDo::getDeviceName,accessAuthDeviceQryCmd.getName());
        queryWrapper.eq(StringUtils.isNotBlank(accessAuthDeviceQryCmd.getType()),AccessAuthDeviceDo::getType,accessAuthDeviceQryCmd.getType());
        if(ObjectUtils.isEmpty(accessAuthDeviceQryCmd.getIsRelated())){
            queryWrapper.and(w->w.isNull(AccessAuthDeviceDo::getAccessAuthId).or().eq(AccessAuthDeviceDo::getAccessAuthId,accessAuthDeviceQryCmd.getAccessAuthId()));
        }else {
            if(accessAuthDeviceQryCmd.getIsRelated()){
                queryWrapper.eq(AccessAuthDeviceDo::getAccessAuthId,accessAuthDeviceQryCmd.getAccessAuthId());
            }else {
                queryWrapper.isNull(AccessAuthDeviceDo::getAccessAuthId);
            }
        }
        Page<AccessAuthDeviceDo> page = new Page<>(accessAuthDeviceQryCmd.getPageIndex(),accessAuthDeviceQryCmd.getPageSize());
        Page<AccessAuthDeviceDo> selectPage = accessAuthDeviceMapper.selectPage(page, queryWrapper);
        return AccessAuthDeviceConvert.INSTANCE.convertAccessAuthDeviceDoPageToAccessAuthDevices(selectPage);
    }

	@Override
    public Response update(AccessAuthDevice accessAuthDevice) {
        LambdaUpdateWrapper<AccessAuthDeviceDo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(AccessAuthDeviceDo::getAccessAuthId,accessAuthDevice.getAccessAuthId());
        accessAuthDeviceMapper.update(null,updateWrapper);
        return Response.buildSuccess();
    }

	@Override
	public List<AccessAuthDevice> getByDeviceIds(List<Long> deviceIds) {
		LambdaQueryWrapper<AccessAuthDeviceDo> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.in(AccessAuthDeviceDo::getDeviceId,deviceIds);
		List<AccessAuthDeviceDo> accessAuthDeviceDos = accessAuthDeviceMapper.selectList(queryWrapper);
		return AccessAuthDeviceConvert.INSTANCE.convertAccessAuthDeviceDosToAccessAuthDevices(accessAuthDeviceDos);
	}

	@Override
	public Response updateBatch(List<AccessAuthDevice> accessAuthDevices) {
		List<AccessAuthDeviceDo> accessAuthDeviceDos = AccessAuthDeviceConvert.INSTANCE.convertAccessAuthDevicesToAccessAuthDeviceDos(accessAuthDevices);
		accessAuthDeviceMapper.updateBatch(accessAuthDeviceDos);
		return Response.buildSuccess();
	}

    @Override
    public List<Long> deleteAccessAuthDeviceById(Long id,List<Long> deviceIds) {
        LambdaQueryWrapper<AccessAuthDeviceDo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccessAuthDeviceDo::getAccessAuthId,id);
        queryWrapper.in(CollectionUtils.isNotEmpty(deviceIds),AccessAuthDeviceDo::getDeviceId,deviceIds);
        List<AccessAuthDeviceDo> authDeviceDos = accessAuthDeviceMapper.selectList(queryWrapper);
        if(CollectionUtils.isNotEmpty(authDeviceDos)){
            List<Long> ids = authDeviceDos.stream().map(AccessAuthDeviceDo::getId).collect(Collectors.toList());
            LambdaUpdateWrapper<AccessAuthDeviceDo> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.in(AccessAuthDeviceDo::getId,ids);
            updateWrapper.set(AccessAuthDeviceDo::getAccessAuthId,null);
            accessAuthDeviceMapper.update(null,updateWrapper);
            return authDeviceDos.stream().map(AccessAuthDeviceDo::getDeviceId).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

    @Override
    public List<Long> getDeviceIdsByAccessAuthId(Long accessAuthId) {
        LambdaQueryWrapper<AccessAuthDeviceDo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccessAuthDeviceDo::getAccessAuthId,accessAuthId);
        List<AccessAuthDeviceDo> authDeviceDos = accessAuthDeviceMapper.selectList(queryWrapper);
        if(CollectionUtils.isNotEmpty(authDeviceDos)){
            return authDeviceDos.stream().map(AccessAuthDeviceDo::getDeviceId).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

    @Override
    public void updateBatchDevice(List<AccessAuthDevice> updateList) {
        if(CollectionUtils.isNotEmpty(updateList)){
            List<AccessAuthDeviceDo> accessAuthDeviceDos = AccessAuthDeviceConvert.INSTANCE.convertAccessAuthDevicesToAccessAuthDeviceDos(updateList);
            accessAuthDeviceMapper.updateBatch(accessAuthDeviceDos);
        }

    }

    @Override
    public AccessAuthDevice getByDeviceId(String deviceId) {
        LambdaQueryWrapper<AccessAuthDeviceDo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccessAuthDeviceDo::getDeviceId,Long.parseLong(deviceId));
        AccessAuthDeviceDo accessAuthDeviceDo = accessAuthDeviceMapper.selectOne(queryWrapper);
        return AccessAuthDeviceConvert.INSTANCE.convertAccessAuthDeviceDoToAccessAuthDevice(accessAuthDeviceDo);
    }

    @Override
    public void deleteByDeviceId(Long deviceId) {
        LambdaQueryWrapper<AccessAuthDeviceDo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccessAuthDeviceDo::getDeviceId,deviceId);
        accessAuthDeviceMapper.delete(queryWrapper);
    }

    @Override
    public List<AccessAuthDevice> selectByDeviceIds(Set<Long> deviceIds) {
        LambdaQueryWrapper<AccessAuthDeviceDo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(AccessAuthDeviceDo::getDeviceId,deviceIds);
        List<AccessAuthDeviceDo> accessAuthDeviceDos = accessAuthDeviceMapper.selectList(queryWrapper);
        return AccessAuthDeviceConvert.INSTANCE.convertAccessAuthDeviceDosToAccessAuthDevices(accessAuthDeviceDos);
    }
}
